home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Cool Demos, SDKs, & Tools / Demos⁄Tools⁄Offers / Alpha ƒ / Tcl / Packages / compare.tcl next >
Text File  |  1999-02-24  |  4KB  |  143 lines

  1. #========================(install)==========================================
  2. # Compare Windows.
  3. # Simplified (and improved) version of David C. Black's 'compare-windows'.
  4. # Modified by Mark Nagata, 2/23/93, corrected, 2/24/93.
  5. # Sped-up version, 2/25/93.
  6. #
  7. # The return position bug in David's routine (when $patt != "") 
  8. # is fixed in this version.
  9. # Vince renamed a couple of things and added the 'package' stuff so
  10. # this works smoothly with the new Alpha Tcl scheme.  The bindings
  11. # can now be adjusted via a preferences dialog.  Also rewrote a few
  12. # bits to try to avoid window-toggling.
  13. #===========================================================================
  14.  
  15. alpha::extension compareWindows 0.23 {
  16.     namespace eval compare {}
  17.     menu::insert Utils submenu 0 compare
  18.     menu::insert "compare" items end windowsInPlace
  19.     hook::register requireOpenWindowsHook [list compare windowsInPlace] 2
  20.     newPref binding findDifference "/`«X»" compareWindows "" compare::windowsInPlace
  21.     newPref binding findDifferenceIgnoringSpace "/1«X»" compareWindows "" compareOpt
  22.     newPref binding findNextDifference "<U/`«X»" compareWindows "" compareNext
  23.     newPref binding findNextDifferenceIgnoringSpace "<U/1«X»" compareWindows "" compareOptNext
  24.     package::addPrefsDialog compareWindows
  25. }
  26. ####
  27. # On my Extended Keyboard (where the backquote key is to the left of the 
  28. # "1" key), I Bind prefix-(shift)-backquote to 'compare(Next)' and
  29. # prefix-(shift)-1 to 'compareOpt(Next)', as in the above.
  30. # On my Powerbook keyboard (where nothing is to the left of the "1" key),
  31. # I Bind prefix-(shift)-1 to 'compare(Next)' and
  32. # prefix-(shift)-2 to 'compareOpt(Next)', respectively.
  33. ####
  34.  
  35. proc compareOpt {} {
  36.     compare::windowsInPlace -w
  37. }
  38.  
  39. proc compare::windowsInPlace {args} {
  40.     if {[lindex $args 0] == "-w"} {
  41.     set patt "\[ \t\n\r\]+"
  42.     } else {
  43.     set patt {}
  44.     }
  45.     
  46.     set files [winNames -f]
  47.     if {[llength $files] < 2} {
  48.     alertnote "If you want to Compare texts, you need two windows."
  49.     return
  50.     }
  51.     
  52.     watchCursor
  53.     for {set i 1} {$i < 3} {incr i} {
  54.     set wn($i) [lindex $files [expr {$i -1}]]
  55.     set wp($i) [getPos -w $wn($i)]
  56.     select -w $wn($i) $wp($i) $wp($i)
  57.     set wrt($i) [getText -w $wn($i) $wp($i) [maxPos -w $wn($i)]]
  58.     set wt($i) $wrt($i)
  59.     if {$patt != ""} {
  60.         regsub -all $patt $wt($i) " " wt($i)
  61.     }
  62.     }
  63.     
  64.     # Exactly equal
  65.     if {$wt(1) == $wt(2)} {
  66.     alertnote "The windows match from cursors to ends."
  67.     return
  68.     }
  69.     
  70.     # Only consider smaller of two strings
  71.     set siz [string length $wt(1)]
  72.     if {$siz > [string length $wt(2)]} {
  73.     set siz [string length $wt(2)]
  74.     }
  75.     
  76.     # Equal except for added stuff
  77.     set l [expr {$siz-1}]
  78.     if {[string range $wt(1) 0 $l] == [string range $wt(2) 0 $l]} {
  79.     set beg $siz
  80.     set offset(1) $beg
  81.     set offset(2) $beg
  82.     } else {
  83.     set beg 0
  84.     
  85.     while {$siz} {
  86.         set siz [expr {$siz/ 2}]
  87.         set end [expr {$beg+$siz}]
  88.         if {[string range $wt(1) $beg $end] == [string range $wt(2) $beg $end]} {
  89.         incr beg $siz
  90.         incr beg
  91.         }
  92.     }
  93.     set offset(1) $beg
  94.     set offset(2) $beg
  95.     }
  96.     for {set i 2} {$i > 0} {incr i -1} {
  97.     set count $offset($i)
  98.     set pos [pos::math $wp($i) + $count]
  99.     if {$patt != ""} {
  100.         set ans [string range $wt($i) 0 [expr {$offset($i)-1}]]
  101.         set lans [string length $ans]
  102.         set tt [string range $wrt($i) 0 [expr {$count-1}]]
  103.         regsub -all $patt $tt " " tt
  104.         set ltt [string length $tt]
  105.         while {$ltt < $lans} {
  106.         incr count [expr {$lans-$ltt}]
  107.         set pos [pos::math $pos + [expr {$lans-$ltt}]]
  108.         message $pos
  109.         set tt [string range $wrt($i) 0 [expr {$count-1}]]
  110.         regsub -all $patt $tt " " tt
  111.         set ltt [string length $tt]
  112.         }
  113.     }
  114.     
  115.     set pos [expr [pos::compare $pos > [maxPos -w $wn($i)]] ? [maxPos -w $wn($i)] : $pos]
  116.     display -w $wn($i) [pos::math $pos - 1]
  117.     select -w $wn($i) $pos [pos::math $pos + 1]
  118.     refresh $wn($i)
  119.     }
  120.     message "difference found"
  121.     return
  122. }
  123.  
  124. proc compareNext {} {
  125.     endOfLine
  126.     catch {bringToFront [lindex [winNames -f] 1]}
  127.     endOfLine
  128.     compare::windowsInPlace
  129. }
  130.  
  131. proc compareOptNext {} {
  132.     endOfLine
  133.     catch {bringToFront [lindex [winNames -f] 1]}
  134.     endOfLine
  135.     compare::windowsInPlace -w
  136. }
  137.  
  138.  
  139.  
  140.